from marshmallow import fields
from marshmallow_sqlalchemy import SQLAlchemySchema
from api.database import db
from api.apps.author.models import Author
from api.apps.books.schemas import BookSchema


# 序列接口数据为JSON格式
class AuthorSchema(SQLAlchemySchema):
    class Meta(SQLAlchemySchema.Meta):
        model = Author
        sqlalchemy_session = db.session
        load_instance = True

    # 自定义字段
    id = fields.Integer(dump_only=True)
    name = fields.String(required=True)
    created = fields.String(dump_only=True)
    books = fields.Nested(BookSchema, many=True, only=['title', 'year', 'id'])
